// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.linqingying.gitcode.pullrequest.ui.timeline

import com.linqingying.gitcode.api.data.pullrequest.GCPullRequestState
import com.linqingying.gitcode.api.data.pullrequest.timeline.GCPRTimelineEvent

class GCPRTimelineMergedStateEvents(initialState: GCPRTimelineEvent.State) : GCPRTimelineMergedEvents<GCPRTimelineEvent.State>(), GCPRTimelineEvent.State {
  private val inferredOriginalState: GCPullRequestState = when (initialState.newState) {
    GCPullRequestState.CLOSED -> GCPullRequestState.OPEN
    GCPullRequestState.MERGED -> GCPullRequestState.OPEN
    GCPullRequestState.OPEN -> GCPullRequestState.CLOSED
  }

  init {
    add(initialState)
  }

  override var newState: GCPullRequestState = initialState.newState
    private set

  var lastStateEvent = initialState
    private set

  override fun addNonMergedEvent(event: GCPRTimelineEvent.State) {
    if (newState != GCPullRequestState.MERGED) {
      newState = event.newState
      lastStateEvent = event
    }
  }

  override fun hasAnyChanges(): Boolean = newState != inferredOriginalState
}
